package com.bike.controller.api;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bike.controller.BaseController;
import com.bike.entity.HhBusinessUser;
import com.bike.entity.HhDining;
import com.bike.service.impl.HhDiningServiceImpl;
import com.bike.util.Constant;
import com.bike.util.FileZip;
import com.bike.util.PageData;
import com.bike.vo.PcJsonView;
import com.fasterxml.jackson.annotation.JsonView;

 
@Controller
@RequestMapping(value="/apiDining")
public class HhApiDiningController extends BaseController {
	
		@Autowired
		private HhDiningServiceImpl  hhDiningServiceImpl;
		
		 /**
		  * @param request
		  * @param model
		  * @return
		  */
		@RequestMapping("list")
		@ResponseBody
		@JsonView(PcJsonView.class)
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			pd.put("businessId", businessUser.getBusinessId());
			PageData rows=  hhDiningServiceImpl.selectHhDiningList(pd);
			PageData result=getSuccessPd(rows);
			return result;
		}
		
		/**
		 * 新增桌牌
		 * @param request
		 * @param name  桌牌名称
		 * @param businessId  商家id
		 * @param sorts 排序
		 * @param note  说明
		 * @return
		 */
		@RequestMapping("saveDining")
		@ResponseBody
		public PageData saveDining(HttpServletRequest request,HhDining hhDining,Model model){
			PageData pd=PageData.getToPageData(request);
			
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
		 
//			if(pd.getLong("businessId")==businessUser.getBusinessId()){
				int result= hhDiningServiceImpl.saveOrUpdate(request,pd, hhDining);
				if(result>0){
					return getSuccessPd();
				}
//			}
			return getErrorPd("系统异常,请重试");
			
		}
		/**
		 * 导入桌牌
		 * @param request
		 * @param diningStr  桌牌名称 $ 分隔
		 * @return
		 */
		@RequestMapping("execDining")
		@ResponseBody
		public PageData execDining(HttpServletRequest request,HhDining hhDining,Model model){
			PageData pd=PageData.getToPageData(request);
			
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			
			String realPath = request.getSession().getServletContext().getRealPath("");//项目所在路径
//			if(pd.getLong("businessId")==businessUser.getBusinessId()){
				new Thread( new Runnable() {
					public void run() {
						String[] dinings=pd.getString("diningStr").split("_");
						for(String dining:dinings){
							if(dining.equals("")){
								continue;
							}else{
								int result= hhDiningServiceImpl.saveExecDining(realPath,pd,dining);
								try {
									Thread.sleep(3000);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
						}
						
					}
				}).start();
				 
					return getSuccessPd();
//			}
//			return getErrorPd("系统异常,请重试");
			
		}
		
		/**
	 	 * 桌牌打包下载
	 	 * @param request
	 	 * @param response
	 	 */
 		 @RequestMapping(value = "/zipDining")
 	    public void zipDining(HttpServletRequest request,HttpServletResponse response) {
 			 
 		 	try {
		 		PageData pd =PageData.getToPageData(request);
		 		HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
		 		pd.put("businessId", businessUser.getBusinessId());
				List<HhDining> list = hhDiningServiceImpl.listAll(pd);
				List<InputStream> iss=new ArrayList<>();
				 
				for(HhDining pdList:list){
					iss.add( new FileInputStream(new File(Constant.filePath.concat(pdList.getImg()))));
				}
				FileZip.zipAndDownload("dining.zip", response, iss,".".concat(Constant.format));
			} catch (Exception e) {
				e.printStackTrace();
			}
 	    }
		
 		/**
 		 * 删除桌牌
 		 */
 		@RequestMapping("deleteDining")
		@ResponseBody
		public PageData deleteDining(HttpServletRequest request,HhDining hhDining,Model model){
			PageData pd=PageData.getToPageData(request);
			
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			HhDining dining=hhDiningServiceImpl.selectByHhDiningId(pd.getLong("diningId"));
			
			if(dining.getBusinessId()==businessUser.getBusinessId()){
				int result= hhDiningServiceImpl.deleteHhDiningById(dining.getDiningId());
				if(result>0){
					return getSuccessPd();
				}
			}
			return getErrorPd("系统异常,请重试");
			
		}
	 
}
